/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-17
 * Time: 22:56
 */
#ifndef CODSEEGMENT_DIAGRAMSCENE_H
#define CODSEEGMENT_DIAGRAMSCENE_H

#include "DiagramBaseItem.h"
#include "Arrow.h"
#include <QtWidgets>

class DiagramScene
        : public QGraphicsScene
{
    Q_OBJECT
public:
    enum SceneMode
    {
        InsertRectItem = 0,
        InsertCircleItem,
        InsertDiamondItem,
        InsertStraightLineItem,
        InsertRightAngleLineItem,
        MoveItem
    };
    explicit DiagramScene(QObject *parent = nullptr);
    DiagramScene(QMenu *menu, QObject *parent = nullptr);

    ~DiagramScene() override;

    QFont font() const
    { return font_; }

    QColor textColor() const
    { return text_color_; }

    QColor itemColor() const
    { return item_color_; }

    QColor lineColor() const
    { return line_color_; }

    void setLineColor(const QColor &color);

    void setTextColor(const QColor &color);

    void setItemColor(const QColor &color);

    void setFont(const QFont &font);


    void SetSceneMode(SceneMode mode)
    { scene_mode_ = mode; }

    void SetDefaultMode();

    void DeleteOneItem(DiagramBaseItem * item);

    void DeleteOneArrow(Arrow * arrow);

    void DeleteArrowByStartItemID(const QString id);

    void UpdateArrow(const QString id);
public slots:

    void SlotNewRectItem();
    void SlotNewCircleItem();
    void SlotNewDiamondItem();
    void SlotAutoAlignItem();
private:
    bool isItemChange(int type);
    void InitEvent();
    QRectF ChildrenItemsRect(DiagramBaseItem * _item);
protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

    virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;

private:

    SceneMode                   scene_mode_;
    bool                        left_button_down_;
    QPointF                     mouse_press_point_;
    QGraphicsLineItem *         temp_line_;     //过程中删除  析构中无需删除
    QColor                      text_color_;
    QColor                      item_color_;
    QColor                      line_color_;
    QFont                       font_;
    //QMenu *                     diagram_menu_;
    DiagramBaseItem *           new_item_;      // 添加入scene中，交由scene处理
    DiagramBaseItem *           root_item_;
public:
    DiagramBaseItem *GetRootItem() const
    { return root_item_; }

    void AddNewItem(DiagramBaseItem * _item);

    void AddNewItem(const QString & _title_,bool _is_root = false);

    void ToSqlString(DiagramBaseItem * _parent_item,QString & _result);

    void FromSqlString(QByteArray & query_result , bool _is_root = false);
private:
    // 添加入scene中，交由scene处理

    QList<Arrow *>              arrows_;
    QMap<QString,DiagramBaseItem *> items_;
//    QList<DiagramBaseItem *>    items_;
};


#endif //CODSEEGMENT_DIAGRAMSCENE_H
